
YOSYS := yosys

ifeq ($(strip $(SUPRA_PATH)),)
$(error "%SUPRA_PATH% no find!")
endif

_SUPRA_PATH := $(subst \,/,$(SUPRA_PATH))

PROJ_PATH := verilog
PROJ_NAME := usb
TOP_NAME := usb

AF := 			$(_SUPRA_PATH)/bin/af.exe
OPENOCD_CMD :=	$(_SUPRA_PATH)/openocd/bin/openocd_cmd.bat
OPENOCD 	:=	$(_SUPRA_PATH)/openocd/bin/openocd.exe
WRITE_PRG_CMD := \
	-c "variable ADAPTER cmsis-dap; \
	variable CONNECT_UNDER_RESET 1" -c "variable ADAPTER_SPEED 12000" \
	-f $(_SUPRA_PATH)/pio/agrv2k.cfg \
	-c "agrv progress 0" -c "reset init" \
	-c "agrv options_erase; reset init" \
	-c "agrv write_fpga_config {verilog/$(PROJ_NAME)/$(PROJ_NAME).bin}" \
	-c "reset run" \
	-c exit
	# -c "flash erase_address pad 0x80000000 4096"
	# -c "flash filld 0x80000000 0xaa234371030002b7 1"
	# -c "flash filld 0x80000008 0xbff5105000730062 1"
# AGRV2KL48
# AGRV2KL64
# AGRV2KL100
# DEVICE = AGRV2KL64
DEVICE = AGRV2KL48

# 引脚定义文件. 如slave_ahb64.asf
# ASF_FILE := $(PROJ_NAME)64.asf
ASF_FILE := $(PROJ_NAME)48.asf

V_FILES := \
	$(PROJ_PATH)/def.v \
	$(PROJ_PATH)/$(PROJ_NAME)/$(PROJ_NAME).v \
	$(PROJ_PATH)/core.v

MASTER_PRG := $(PROJ_PATH)/$(PROJ_NAME)/$(PROJ_NAME)_master.prg
VQM_FILE := $(PROJ_PATH)/$(PROJ_NAME)/$(PROJ_NAME).vqm

CLEAN_FILES := \
	$(PROJ_PATH)/$(PROJ_NAME)/*.prg \
	$(PROJ_PATH)/$(PROJ_NAME)/*.vqm \
	$(PROJ_PATH)/$(PROJ_NAME)/$(PROJ_NAME)_routed.v \
	$(PROJ_PATH)/$(PROJ_NAME)/$(PROJ_NAME)_slave.rbf \
	$(PROJ_PATH)/$(PROJ_NAME)/$(PROJ_NAME).bin \
	$(PROJ_PATH)/$(PROJ_NAME)/$(PROJ_NAME)_master.bin \
	$(PROJ_PATH)/$(PROJ_NAME)/alta_db/ \
	$(PROJ_PATH)/$(PROJ_NAME)/alta_logs/

vqm:$(VQM_FILE)
prg:$(MASTER_PRG)
write_prg:$(MASTER_PRG)
	$(OPENOCD_CMD) $(WRITE_PRG_CMD)
clean:
	-rm -r $(CLEAN_FILES)

$(VQM_FILE):$(V_FILES) $(PROJ_PATH)/$(PROJ_NAME)/$(ASF_FILE)
	$(YOSYS) -p "read_verilog $(V_FILES)" -p "synth_intel -family cycloneive -top $(TOP_NAME) -iopads" -p "write_verilog -simple-lhs -attr2comment -defparam -nohex -renameprefix syn_ $(VQM_FILE)"

$(MASTER_PRG):$(VQM_FILE)
	cd $(PROJ_PATH)/$(PROJ_NAME)/ &&$(AF) -B --batch --mode SYNPLICITY -X "set DEVICE \"$(DEVICE)\"" -X "set ASF_FILE \"$(ASF_FILE)\"" -X "set DESIGN \"$(TOP_NAME)\"" -X "set QUARTUS_SDC true" -X "set FITTING timing_more" -X "set FITTER hybrid" -X "set EFFORT highest" -X "set HOLDX default" -X "set SKEW basic"

